package com.example.testapp

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.navigation.Navigation
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import com.example.testapp.databinding.ActivityMainBinding
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private var doubleBackToExitPressedOnce = false
    private val handler = Handler(Looper.getMainLooper())
    private val resetBackPressedRunnable = Runnable { doubleBackToExitPressedOnce = false }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
        // 设置状态栏颜色与背景一致 - 在setContentView之后设置
        val window = window
        // 使用新API设置状态栏颜色
        window.statusBarColor = androidx.core.content.ContextCompat.getColor(this, R.color.background_grey)

        // 使用WindowInsetsControllerCompat设置状态栏文字颜色为深色（适用于浅色背景）
        // 这是一个兼容性API，可以在所有Android版本上工作，避免使用过时的API
        val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
        // 设置状态栏文字和图标为深色
        windowInsetsController.isAppearanceLightStatusBars = true
        
        // 确保状态栏正确显示
        WindowCompat.setDecorFitsSystemWindows(window, true)

        // 隐藏ActionBar
        supportActionBar?.hide()

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration.Builder(
            R.id.navigation_home, R.id.navigation_list, R.id.navigation_stats, R.id.navigation_mine
        ).build()
        val navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main)
        NavigationUI.setupWithNavController(binding.navView, navController)

        // 设置双击返回退出
        setupDoubleBackToExit()
    }

    private fun setupDoubleBackToExit() {
        // 注册返回键回调
        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                if (doubleBackToExitPressedOnce) {
                    // 第二次点击，退出应用
                    finish()
                } else {
                    // 第一次点击，显示提示
                    doubleBackToExitPressedOnce = true
                    Toast.makeText(this@MainActivity, "再次滑动返回键退出应用", Toast.LENGTH_SHORT).show()

                    // 2秒后重置状态
                    handler.postDelayed(resetBackPressedRunnable, 2000)
                }
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        // 移除回调，防止内存泄漏
        handler.removeCallbacks(resetBackPressedRunnable)
    }
}